package com.sservice.commonBusiness.controller.web;

import com.github.pagehelper.PageHelper;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.commonBusiness.e.DataR;
import com.sservice.commonBusiness.e.Statistic;
import com.sservice.commonBusiness.service.DataRService;
import com.sservice.commonBusiness.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by byte_su@163.com on 2018/02/05.
 * 统计信息
 */
@Controller
@RequestMapping("/statistic1/")
public class Statistic1Controller {

    @Autowired
    public StatisticService statisticService;

    @Autowired
    DataRService dataRService;



    @RequestMapping(value = "statistic", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult statistic(Statistic statistic, String sourceId, String comId) {
        if (statistic.getTargetId() == null || statistic.getType1() == null) {
            return new BaseResult(false, "参数错误", 403);
        }
        statisticService.plus(statistic.getTargetId(), statistic.getType1(),
                statistic.getRemark() != null ? statistic.getRemark() : "view");
        if ("propagate-tpl".equals(statistic.getType1())) {
            DataR dataR = new DataR();
            dataR.setType1("popular-dr");
            dataR.setTargetId(Integer.valueOf(statistic.getTargetId()));
            dataR.setSourceId(Integer.valueOf(sourceId));
            //dataR
            List<DataR> list = dataRService.queryBy(dataR);
            if (list.size() == 1) {
                DataR d = list.get(0);
                d.setComId((Integer.parseInt(d.getComId()) + 1) + "");
                dataRService.updateByPrimaryKeySelective(d);
            }

        }

        return new BaseResult(true, "ok", 200);
    }

    /**
     * 设置字段值
     *
     * @param statistic
     * @return
     */
    @RequestMapping(value = "set", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult set(Statistic statistic) {
        statisticService.setField(statistic);
        return new BaseResult(true, "ok", 200);
    }


    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(@RequestParam Integer id) {
        Statistic statistic = statisticService.selectByPrimaryKey(id);
        return new BaseResult(true, statistic);
    }

    @RequestMapping("get")
    @ResponseBody
    public BaseResult getStatistic(Statistic statistic) {
        Statistic statistic1 = statisticService.selectBy(statistic.getTargetId(), statistic.getType1());
        return new BaseResult(true, statistic1);
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        return null;
    }
}
